//
//  MXFingerprintVC.m
//  MXLoginMethodsDomo
//
//  Created by 马彦 on 16/10/8.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXFingerprintVC.h"
#import "MXFingerprintTableCell.h"
#import "UITableViewCell+Cellutils.h"
#import "MXPromptView.h"

#import "WJTouchID.h"
#import "MXTouchIDPassword.h"

@interface MXFingerprintVC () <UITableViewDelegate,UITableViewDataSource,MXFingerprintTableCellDelegate,WJTouchIDDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) WJTouchID *touchID;

@end

@implementation MXFingerprintVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)dealloc {
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
    WJTouchID *touchid = [[WJTouchID alloc] init];
    self.touchID = touchid;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *FingerprintCell = @"FingerprintCell";
    
    MXFingerprintTableCell *cell = [tableView dequeueReusableCellWithIdentifier:FingerprintCell];
    if (!cell) {
        cell = [[MXFingerprintTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FingerprintCell];
    }
    cell.delegate = self;
    cell.titleStr = @"指纹登陆";
    [cell addBottomHorizontalLine:5];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CellHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}


#pragma mark - MXFingerprintTableCellDelegate

- (void)fingerprintWithSwitch:(UISwitch *)swit {
    
    if (swit.on) {
        [self.touchID startWJTouchIDWithMessage:WJNotice(@"通过Home键验证已有手机指纹", @"The existing mobile phone fingerprints are verified through the Home button") fallbackTitle:WJNotice(@"验证登录密码", @"Verify password") delegate:self];
    } else {
        [self.touchID startWJTouchIDWithMessage:WJNotice(@"通过Home键验证已有手机指纹", @"The existing mobile phone fingerprints are verified through the Home button") fallbackTitle:WJNotice(@"验证登录密码", @"Verify password") delegate:self];
    }
}


#pragma makr - WJTouchIDDelegate

/**
 *  TouchID验证成功
 *
 *  (English Comments) Authentication Successul  Authorize Success
 */
- (void)WJTouchIDAuthorizeSuccess {
//    
//    NSLog(@"验证成功或者失败");
    MXPromptView *promptView = [[MXPromptView alloc] init];
    if ([MXTouchIDPassword isEmptyPassword]) {
        NSLog(@"关闭失败");
        [promptView promptViewAlertViewWithView:self.view andTitle:@"验证成功" andStyle:PromptViewStyleSuccessful];
        [MXTouchIDPassword setNewPassword:@"MXTouchIDPassword"];
    } else {
        NSLog(@"关闭成功");
        [promptView promptViewAlertViewWithView:self.view andTitle:@"关闭成功" andStyle:PromptViewStyleSuccessful];
        [MXTouchIDPassword removePassword];
    }
    
}


/**
 * TouchID验证失败
 * (English Comments) Authentication Failure
 */
- (void)WJTouchIDAuthorizeFailure {
    NSLog(@"touchID 验证失败");
    MXPromptView *promptView = [[MXPromptView alloc] init];
    [promptView promptViewAlertViewWithView:self.view andTitle:@"验证失败" andStyle:PromptViewStyleFailure];
    promptView.block = ^{
        [self.tableView reloadData];
    };
    promptView.block();
}


/**
 * 取消TouchID验证 (用户点击了取消)
 * (English Comments) Authentication was canceled by user (e.g. tapped Cancel button)
 */
- (void)WJTouchIDAuthorizeErrorUserCancel {
    NSLog(@"取消TouchID验证(用户点击了取消)");
    
    [self.tableView reloadData];
}


/**
 * 在TouchID 对话框中点击输入密码按钮
 * (English Comments) User tapped the fallback button
 */
- (void)WJTouchIDAuthorizeErrorUserFallback {
    NSLog(@"在TouchID 对话框中点击输入密码按钮");
    
    [self.tableView reloadData];
}

/**
 * 在验证的TouchID的过程中被系统取消，例如突然来电话、按了Home键、锁屏...
 * (English Comments) Authentication was canceled by system (e.g. another application went to foreground).
 */
- (void)WJTouchIDAuthorizeErrorSystemCancel {
    NSLog(@"在验证的TouchID 的过程中被系统取消");
    
    [self.tableView reloadData];
}

/**
 * 无法启用TouchID,设备没有设置密码
 * (English Comments) Authentication could not start, because passcode is not set on the device.
 */
- (void)WJTouchIDAuthorizeErrorPasscodeNotSet {
    NSLog(@"无法启用TouchID, 设备没有设置密码");
    
    [self.tableView reloadData];
}

/**
 * 设备没有录入TouchID，无法启用TouchID
 * (English Comments) Authentication could not start because Touch ID has no enrolled fingers
 */
- (void)WJTouchIDAuthorizeErrorTouchIDNotEnrolled {
    NSLog(@"设备没有录入TouchID，无法启用TouchID");
    
    [self.tableView reloadData];
}

/**
 * 该设备的TouchID无效
 * (English Comments) Authentication could not start, because Touch ID is not available no the device.
 */
- (void)WJTouchIDAuthorizeErrorTouchIDNotAvailable {
    NSLog(@"该设备的TouchID无效");
    
    [self.tableView reloadData];
}


/**
 * 多次连续使用Touch ID失败，Touch ID被锁，需要用户输入密码解锁
 * (English Comments) Authentication was not successful, because there were too many failed Touch ID attempts and Touch ID is now locked. Passcode is required to unlock Touch ID , e.g. evaluating LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite.
 */
- (void)WJTouchIDAuthorizeErrorTouchIDLockout {
    NSLog(@"多次连续使用TouchID失败,Touch ID被锁, 需要用户输入密码解锁");
    MXPromptView *promptView = [[MXPromptView alloc] init];
    [promptView promptViewAlertViewWithView:self.view andTitle:@"使用失败" andStyle:PromptViewStyleFailure];
    
    [self.tableView reloadData];
}


/**
 * 当前软件被挂起取消了授权(如突然来了电话，应用进入前台)
 * (English Comments) Authentication was canceled by application (e.g.invalidate was called while authentication was inprogress).
 */
- (void)WJTouchIDAuthorizeErrorAppCancel {
    NSLog(@"当前软件被挂起取消授权");
    
    [self.tableView reloadData];
}

/**
 * 当前软件被挂起取消了授权(授权过程中，LAContext对象被释)
 * (English Comments) LAContext passed to this call has been previously invalidated.
 */
- (void)WJTouchIDAuthorizeErrorInvalidContext {
    NSLog(@"当前软件被挂起取消了授权");
    
    [self.tableView reloadData];
    
}

/**
 *  当前设备不支持指纹识别
 *
 *  (English Comments) The current device does not support fingerprint identification
 */
- (void)WJTouchIDIsNotSupport {
    NSLog(@"当前设备不支持指纹识别");
    
    [self.tableView reloadData];
}


#pragma mark -lazy

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = RGBA_Color(0.97, 0.97, 0.97, 1.0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end
